<?php
class Admin_TourController extends Zend_Controller_Action
{
	public function indexAction()
	{
		// Forward to list action
		$this->_forward('list');
	}
	
	public function listAction()
	{
		$req = $this->getRequest();
		$page = $req->getParam('page', 1);
		$config = My_Globals::getConfiguration();
		$limit = $config->site->admin->limit;		
		$tours = Tour::getInstance()->getTourList($page, $limit);
		$this->view->tours = $tours;
		$this->view->page = $page;
		$this->view->limit = $limit;
		$this->view->amount = Tour::getInstance()->getAmount();
	}
	
	public function editAction()
	{
		$params = $this->_getAllParams();
		
		// Check tour exist
		if($params['tour_id'] == 0) return false;
		$tour = Tour::getInstance()->getTour($params['tour_id']);
		if(!$tour) return false;
		
		if($this->getRequest()->getMethod() == "POST") {
			$error = false;
			if(strlen($params['tour_name']) == 0) {
				$error = true;
			}
			if(strlen($params['tour_price']) == 0) {
				$error = true;
			}
			if(strlen($params['tour_intro']) == 0) {
				$error = true;
			}
			if(strlen($params['tour_no']) == 0) {
				$error = true;
			}
			if($params['tour_departure'] == 0) {
				$error = true;
			}
			
			if($error == false) {
				$image = My_Upload::upload('tour_image', $tour['created']);
				if($image != null) {
					$params['tour_image'] = $image;
				} else {
					$params['tour_image'] = null;
				}
				// Save to DB
				$params['updated'] = time();
				$tourId = Tour::getInstance()->saveTour($params);
				if($tourId > 0 && isset($params['category_id'])) {
					Tour::getInstance()->saveCategoriesTours($tourId, $params['category_id']);
				}
				$this->_redirect($this->view->serverUrl().$this->view->url(array('module' => 'admin', 'controller' => 'tour', 'action' => 'list'), null, true));
			}
		}
		$categories = Category::getInstance()->getCategoryList();
		$categoriesSelected = Tour::getInstance()->getCategoriesByTourId($params['tour_id']);		
		$locations = Location::getInstance()->getLocationList();
		
		$this->view->tour = $tour;
		$this->view->params = $params;
		$this->view->categories = $categories;
		$this->view->categoriesSelected = $categoriesSelected;
		$this->view->locations = $locations;
	}
	
	public function createAction()
	{
		$params = $this->_getAllParams();
		
		if($this->getRequest()->getMethod() == "POST") {
			$error = false;
			if(strlen($params['tour_name']) == 0) {
				$error = true;
			}
			if(strlen($params['tour_price']) == 0) {
				$error = true;
			}
			if(strlen($params['tour_intro']) == 0) {
				$error = true;
			}
			if(strlen($params['tour_no']) == 0) {
				$error = true;
			}
			if($params['tour_departure'] == 0) {
				$error = true;
			}
			
			if($error == false) {
				$params['created'] = time();
				// Upload image
				$image = My_Upload::upload('tour_image', $params['created']);
				if($image != null) {
					$params['tour_image'] = $image;
				} else {
					$params['tour_image'] = null;
				}
				// Get User
				$userInfo = new Zend_Session_Namespace('admin');
				$params['user_id'] = $userInfo->user['user_id'];
				
				// Save to DB
				$params['updated'] = time();
				$tourId = Tour::getInstance()->saveTour($params);
				if($tourId > 0 && isset($params['category_id'])) {
					Tour::getInstance()->saveCategoriesTours($tourId, $params['category_id']);
				}
				$this->_redirect($this->view->serverUrl().$this->view->url(array('module' => 'admin', 'controller' => 'tour', 'action' => 'list'), null, true));
			}
		}
		$categories = Category::getInstance()->getCategoryList();
		$locations = Location::getInstance()->getLocationList();
		// Asign to viewer
		$this->view->params = $params;
		$this->view->categories = $categories;
		$this->view->locations = $locations;
	}
	
	public function orderAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();		
		foreach($params as $k => $v) {
			$item = str_replace("order_", "", $k);
			if($item > 0 && $v >= 0) {
				$list = array(
					'tour_id' => $item,
					'ordering' => $v
				);
				if(Tour::getInstance()->saveTour($list)) {
					$this->_returnState(1);
				}
			}
		}
		$this->_returnState(0);
	}
	
	public function deleteAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = (int)$this->getRequest()->getParam('tour_id');
		if( Tour::getInstance()->deleteTour($id) ) {
			return $this->_returnState(1);
		}
		return $this->_returnState(0);
	}
	
	public function approveAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		if($params['tour_id'] > 0) {
			$list = array(
				'tour_id' => $params['topic_id'],
				'active' => (($params['state'] == 0)?1:0)
			);
			if(Tour::getInstance()->saveTour($list)) {
				return $this->_returnState(1);
			}
		}
		return $this->_returnState(0);
	}
	
	private function _returnState($state = 1)
	{
		echo json_encode(array('status' => $state));
		exit;
	}
}